 package com.lfk.justwetools.Component.ReadView;

import com.lfk.justwetools.Utils.Log;
import ohos.agp.components.Image;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.render.Texture;
import ohos.agp.utils.Point;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;
import ohos.media.image.PixelMap;
import ohos.agp.render.Canvas;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;
import ohos.multimodalinput.event.TouchEvent;
import ohos.agp.components.Component;

import com.lfk.justwetools.ResourceTable;

 public class ReadView extends Component implements Component.DrawTask, Component.TouchEventListener {
	private PixelMap mCurrentPageBitmap;
	private Canvas mCurrentPageCanvas;
	private PageFactory pagefactory;
	private int font_size = 60;
	private Preferences sp;
	private int[] position = new int[]{0, 0};
	private int width;
	private int height;

	public ReadView(Context context ,String path) {
		super(context);
		sp = new DatabaseHelper(context).getPreferences("config");
		font_size = sp.getInt("font_size", 60);
		Point screenSize = new Point();
		DisplayManager.getInstance().getDefaultDisplay(context).get().getSize(screenSize);
		width = screenSize.getPointXToInt();
		height = screenSize.getPointYToInt() - 200;
		Log.e(width + ":宽", height + "：高");
		PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
		options.size = new Size(width, height);
		options.pixelFormat = PixelFormat.ARGB_8888;
		options.editable = true;
		mCurrentPageBitmap = PixelMap.create(options);
		mCurrentPageCanvas = new Canvas(new Texture(mCurrentPageBitmap));
		pagefactory = new PageFactory(context,width, height, font_size);
		try {
			position[0] = sp.getInt("begin",0);
			position[1] = sp.getInt("end",0);
			Log.e("start" + position[0], "end" + position[1]);
//			pagefactory.setBgBitmap(new PixelMapElement(
//					context.getResourceManager().getResource(ResourceTable.Media_bg)).getPixelMap());
			Image image = new Image(context);
			image.setPixelMap(ResourceTable.Media_bg);
			pagefactory.setBgBitmap(image.getPixelMap());
			pagefactory.openBook(path, position);
			pagefactory.onDraw(mCurrentPageCanvas);
		} catch (Exception e) {
			// TODO: handle exception
		}

		addDrawTask(this);
		setTouchEventListener(this);
	}

	 @Override
	 public void onDraw(Component component, Canvas canvas) {
		canvas.save();
		canvas.drawPixelMapHolder(new PixelMapHolder(mCurrentPageBitmap), 0, 0, new Paint());
		canvas.restore();
	}

	public void setBackGround(PixelMap bitmap){
		pagefactory.setBgBitmap(bitmap);
	}
	public void setDrawBitMap(PixelMap bitmap){
		mCurrentPageBitmap = bitmap;
	}

	public void setOnPause(){
		position = pagefactory.getPosition();
        sp.putInt("begin", position[0]);
        sp.putInt("end", position[1]);
		sp.flush();
        int fontSize = pagefactory.getTextFont();
		sp.putInt("font_size", fontSize);
		sp.flush();
	}

	public void setFont_size(int font_size) {
		pagefactory.setTextFont(font_size);
	}

	public void setPresent(int present){
		pagefactory.setPercent(present);
	}

	 public int[] getPosition(){
		 return pagefactory.getPosition();
	 }

	public void refresh(){
		pagefactory.onDraw(mCurrentPageCanvas);
		setDrawBitMap(mCurrentPageBitmap);
		invalidate();
	}
	//&& event.getY() > height * 2 / 3

	 @Override
	 public boolean onTouchEvent(Component component, TouchEvent event) {
			if (event.getAction() == TouchEvent.PRIMARY_POINT_DOWN ) {
				if (event.getPointerPosition(0).getX() > width / 2) {
					pagefactory.nextPage();
					pagefactory.onDraw(mCurrentPageCanvas);
					setDrawBitMap(mCurrentPageBitmap);
				} else {
					pagefactory.prePage();
					pagefactory.onDraw(mCurrentPageCanvas);
					setDrawBitMap(mCurrentPageBitmap);
				}
				invalidate();
				return true;
			}
		return false;
	}

}
